package com.jhgsys.internal.hazard.service.impl;

import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.utils.BeanHelper;
import com.jhgsys.internal.hazard.dto.HazardReportMessageDto;
import com.jhgsys.internal.hazard.entity.HazardReportMessage;
import com.jhgsys.internal.hazard.mapper.HazardReportMessageMapper;
import com.jhgsys.internal.hazard.service.IHazardReportMessageService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.system.entity.User;
import com.jhgsys.internal.system.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 *  Service实现
 *
 * @author zhangwei
 * @date 2024-09-12 09:55:48
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class HazardReportMessageServiceImpl extends ServiceImpl<HazardReportMessageMapper, HazardReportMessage> implements IHazardReportMessageService {

    @Autowired
    private IUserService userService;

    /**
     * 查询（分页）
     * @author zhangwei
     * @date 2024-09-12 09:55:48
     * @param request QueryRequest
     * @param hazardReportMessage hazardReportMessage
     * @return IPage<HazardReportMessage>
    */
    @Override
    public IPage<HazardReportMessage> findHazardReportMessagePage(QueryRequest request, HazardReportMessage hazardReportMessage) {
        LambdaQueryWrapper<HazardReportMessage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HazardReportMessage::getDeletedFlg, 0);
        // TODO 设置查询条件

        Page<HazardReportMessage> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author zhangwei
     * @date 2024-09-12 09:55:48
     * @param hazardReportMessage hazardReportMessage
     * @return List<HazardReportMessage>
     */
    @Override
    public List<HazardReportMessageDto> findHazardReportMessageList(HazardReportMessage hazardReportMessage) {
        List<HazardReportMessageDto> dtoList = new ArrayList<>();
	    LambdaQueryWrapper<HazardReportMessage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HazardReportMessage::getDeletedFlg, 0);
		// TODO 设置查询条件
        if(StringUtils.isNotBlank(hazardReportMessage.getReportId())){
            queryWrapper.eq(HazardReportMessage::getReportId,hazardReportMessage.getReportId());
        }
        queryWrapper.orderByAsc(HazardReportMessage::getMessageDate);
        List<HazardReportMessage> list = this.baseMapper.selectList(queryWrapper);
        if(!CollectionUtils.isEmpty(list)){
            dtoList = BeanHelper.copyWithCollection(list, HazardReportMessageDto.class);
            dtoList.forEach(d->{
                User user = userService.findById(d.getUserId());
                d.setAvatar(user.getAvatar());
            });
        }
		return dtoList;
    }

    /**
    * 根据ID查询
    * @author zhangwei
    * @date 2024-09-12 09:55:48
    * @param id
    * @return HazardReportMessage
    */
    @Override
    public HazardReportMessage findHazardReportMessageById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author zhangwei
     * @date 2024-09-12 09:55:48
     * @param hazardReportMessage hazardReportMessage
     */
    @Override
    @Transactional
    public boolean createHazardReportMessage(HazardReportMessage hazardReportMessage) {
        return this.save(hazardReportMessage);
    }

    /**
     * 修改
     * @author zhangwei
     * @date 2024-09-12 09:55:48
     * @param hazardReportMessage hazardReportMessage
     */
    @Override
    @Transactional
    public boolean updateHazardReportMessage(HazardReportMessage hazardReportMessage) {
        return this.updateById(hazardReportMessage);
    }

    /**
     * 逻辑删除
     * @author zhangwei
     * @date 2024-09-12 09:55:48
     * @param hazardReportMessageIds hazardReportMessageIds
     */
    @Override
    @Transactional
    public void deleteHazardReportMessage(String hazardReportMessageIds) {
        boolean result = false;
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        List<String> list = Arrays.asList(hazardReportMessageIds.split(StringPool.COMMA));
        list.forEach(id->{
            HazardReportMessage hazardReportMessage = this.baseMapper.selectById(id);
            hazardReportMessage.setDeletedBy(user.getUserName());
            hazardReportMessage.setDeletedTime(new Date());
            hazardReportMessage.setDeletedFlg(FebsConstant.INVALID);
            this.saveOrUpdate(hazardReportMessage);
        });
	}

    /**
    * 物理删除
    * @author zhangwei
    * @date 2024-09-12 09:55:48
    * @param hazardReportMessageIds hazardReportMessageIds
    */
    @Override
    @Transactional
    public boolean physicallyDeleteHazardReportMessage(String hazardReportMessageIds) {
        List<String> list = Arrays.asList(hazardReportMessageIds.split(StringPool.COMMA));
        LambdaQueryWrapper<HazardReportMessage> wapper = new LambdaQueryWrapper<>();
        wapper.in(HazardReportMessage::getReportMessageId,list);
        return this.remove(wapper);
    }
}
